package io.github.syske.springbootjdbcmybatisdemo.controller;

import io.github.syske.springbootjdbcmybatisdemo.common.CustomIdGenerator;
import io.github.syske.springbootjdbcmybatisdemo.common.GetOPCodeUtil;
import io.github.syske.springbootjdbcmybatisdemo.common.ResultObject;
import io.github.syske.springbootjdbcmybatisdemo.dao.*;
import io.github.syske.springbootjdbcmybatisdemo.entity.TempOp;
import io.github.syske.springbootjdbcmybatisdemo.entity.User;
import io.github.syske.springbootjdbcmybatisdemo.entity.vo.LocateVO;
import io.github.syske.springbootjdbcmybatisdemo.entity.vo.MallVO;
import io.github.syske.springbootjdbcmybatisdemo.service.impl.MallLocateServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.stream.Collectors;

@RestController
public class LocateController {

    @Autowired
    GetOPCodeUtil getOPCodeUtil;
    @Autowired
    MallLocateServiceImpl mallLocateService;

    @Autowired
    TempOpDao tempOpDao;

    @Autowired
    UserDao userDao;
    @Autowired
    MallDao mallDao;

    @Autowired
    LocateDao locateDao;

    @Autowired
    TPDao tpDao;

    @RequestMapping("api/getLocateInfo")
    public Object getLocateInfo(@RequestBody Map<String,Object> param,HttpSession session){
        Object rfid = param.get("m_rfid");
        ResultObject resultObject = new ResultObject();
        if(rfid==null){
            resultObject.message="参数缺少rfid";
            resultObject.code=401;
            return resultObject;
        }
        MallVO mallVO = mallLocateService.getMallByRfid(rfid.toString());
        session.setAttribute("currentLocate",mallVO.locate);
        if(mallVO.rfid !=null) session.setAttribute(mallVO.rfid,mallVO.m_code);
        resultObject.data=mallVO;

        return resultObject;
    }

    @RequestMapping("api/binding")
    public Object updateTPLocate(@RequestBody Map<String,Object> param){
        Object rfid = param.get("rfid");
        ResultObject resultObject = new ResultObject();
        Object locate = param.get("locate");
        Object m_code = param.get("m_code");
        mallDao.updateMallRfid(param);
        tpDao.updateTPRfid(param);
        resultObject.message="绑定成功";
        return resultObject;
    }

    @RequestMapping("api/getLocateByCondition")
    public Object getLocateByCondition(@RequestParam("condition") String condition,@RequestParam("condition1") String condition1 ,
                                       @RequestParam("m_name") String m_name,
                                       @RequestParam("item_code") String item_code){
        if(m_name!=null && !m_name.equals("") && !m_name.equals("undefined")){
            ResultObject resultObject = new ResultObject();
            List<LocateVO> locateByName = locateDao.getLocateByName(m_name);
            if(item_code!=null && !item_code.equals("") && !item_code.equals("undefined")){
                List<LocateVO> filterByItemCode = new ArrayList<>();
                for(int i=0;i<locateByName.size();i++){
                    LocateVO locateVO = locateByName.get(i);
                    if(locateVO.m_type!=null){
                      //  Map<String, Object> itemCodeByMcode = locateDao.getItemCodeByMcode(locateVO.m_code);
                        if(locateVO.m_type.equals(item_code)){
                            filterByItemCode.add(locateVO);
                        }
                    }
                }
//
//                for(int i=0;i<locateByName.size();i++){
//                    LocateVO locateVO = locateByName.get(i);
//                    if(item_code.equals(locateVO.item_code)){
//                        filterByItemCode.add(locateVO);
//                    }
//                }

                resultObject.data= filterByItemCode;
                return resultObject;
            }
            resultObject.data=locateDao.getLocateByName(m_name);
            return resultObject;
        }else if(m_name.equals("") && condition.equals("--") && condition1.equals("--")){
            List<LocateVO> locateByAll = locateDao.getLocateByAll();
            List<LocateVO> filterByItemCode = new ArrayList<>();
            for(int i=0;i<locateByAll.size();i++){
                LocateVO locateVO = locateByAll.get(i);
                if(locateVO.m_type!=null){
//                    Map<String, Object> itemCodeByMcode = locateDao.getItemCodeByMcode(locateVO.m_code);
//                    if(itemCodeByMcode!=null){
//                        if(itemCodeByMcode.get("ITEM_CODE")!=null) locateVO.item_code = itemCodeByMcode.get("ITEM_CODE").toString();
//                    }
                    if(locateVO.m_type.equals(item_code)){
                        filterByItemCode.add(locateVO);
                    }
                }
            }

//            for(int i=0;i<locateByAll.size();i++){
//                LocateVO locateVO = locateByAll.get(i);
//                if(item_code.equals(locateVO.item_code)){
//                    filterByItemCode.add(locateVO);
//                }
//            }
            ResultObject resultObject = new ResultObject();
            resultObject.data= filterByItemCode;
            return resultObject;

        }
        Map<String,Object> param = new HashMap<>();
        param.put("condition",condition);
        String[] split = condition.split("-");
        String first = "";
        String second = "";
        String third = "";
        if(split[0].length()==1){
            first = "00"+split[0];
        }else if(split[0].length()==2) {
            first = "0"+split[0];
        }
        if(split.length>1){
            if(split[1].length()==1){
                second = "00"+split[1];
            }else if(split[1].length()==2) {
                second = "0"+split[1];
            }
        }
        if(split.length>2){
            if(split[2].length()==1){
                third = "0"+split[2];
            }
        }

        List<LocateVO> locateVOList = locateDao.getLocateByCondition(first+"-");
        List<LocateVO> locateVOList2 = new ArrayList<>();
        if(condition1!=null && condition1 !=""){
            String[] split1 = condition1.split("-");
            String first1 = "";
            String second1 = "";
            String third1 = "";
            if(split1[0].length()==1){
                first1 = "00"+split1[0];
            }else if(split1[0].length()==2) {
                first1 = "0"+split1[0];
            }
            if(split1.length>1){
                if(split1[1].length()==1){
                    second1 = "00"+split[1];
                }else if(split1[1].length()==2) {
                    second1 = "0"+split1[1];
                }
            }
            if(split1.length>2){
                if(split1[2].length()==1){
                    third1 = "0"+split1[2];
                }
            }
            if(Integer.parseInt(first1)>=2){
                first1 = "002";
            }
             locateVOList2 = locateDao.getLocateByCondition(first1+"-");
           // locateVOList.addAll(locateVOList2);
        }
        ResultObject resultObject = new ResultObject();
//        List<LocateVO> zonghe = new ArrayList<>();
//        if(m_name != null){
//            List<LocateVO> locateVOList1 = mergerQuchong(locateVOList, locateVOList2);
//            for(int i=0;i<locateVOList1.size();i++){
//                LocateVO vo = locateVOList1.get(i);
//                if(vo.m_name != null){
//                    if( vo.m_name.indexOf(m_name)>0){
//                        zonghe.add(vo);
//                    }
//                }
//            }
//            resultObject.data = zonghe;
//            return resultObject;
//        }else {
            resultObject.data=mergerQuchong(locateVOList,locateVOList2);
            return resultObject;
        //}
    }

    private List<LocateVO> mergerQuchong(List<LocateVO> a,List<LocateVO> b){
        Set<LocateVO> c = new HashSet<>();
         c.addAll(a);
         c.addAll(b);
        ArrayList<LocateVO> locateVOS = new ArrayList<>(c);
        for(int i=0;i<locateVOS.size();i++){
           for(int j=i+1;j<locateVOS.size();j++){
               if(locateVOS.get(j).locate.equals(locateVOS.get(i).locate)){
                   locateVOS.remove(j);
                   j--;
               }
           }
        }
        List<LocateVO> collect = locateVOS.stream().sorted(Comparator.comparing(locateVO -> locateVO.locate))
                .collect(Collectors.toList());

        return collect;
    }

    @RequestMapping("api/getLocateByName")
    public Object getLocateByMallName(@RequestParam("m_name") String m_name){
        List<LocateVO> locateVOS = locateDao.getLocateByName(m_name);
        ResultObject resultObject = new ResultObject();
        resultObject.data = locateVOS;
        return resultObject;
    }

    @RequestMapping("api/outToSTT")
    public Object outToSTT(@RequestBody Map<String,Object> param, HttpSession session){
        Object out_from = param.get("out_from");
        if(out_from!=null && out_from.toString().equals("2")){
            if(param.get("password")!=null && !param.get("password").equals("")){
                User user = (User) session.getAttribute("user");
                if(user!=null){
                    Map<String,Object> map = new HashMap<>();
                    map.put("user_name",user.user_name);
                    map.put("user_password",param.get("password").toString());
                    List<User> list = userDao.getUserByName(map);
                    if(list==null || list.size()==0){
                        ResultObject oo =new ResultObject();
                        oo.code=401;
                        oo.message="密码输入有误";
                        return oo;
                    }
                }else {
                    ResultObject oo =new ResultObject();
                    oo.code=403;
                    oo.message="session过期，请重新登录";
                    return oo;
                }
            }else {
                ResultObject oo =new ResultObject();
                oo.code=401;
                oo.message="登录密码必须填写";
                return oo;
            }
        }

        Object rfid = param.get("rfid");
        Object m_rfid = param.get("m_rfid");
        if(m_rfid != null && rfid==null) rfid = m_rfid;
        if(rfid != null && m_rfid==null) m_rfid = rfid;
        ResultObject resultObject = new ResultObject();
        Object locate = param.get("locate");
        Object m_code = param.get("m_code");
        Object sys_code = param.get("sys_code");
        // param.put("rfid",m_rfid.toString());
        if(m_code != null && !m_code.equals("") && m_rfid != null && !m_rfid .equals("")){
            Map<String,Object> map = new HashMap<>();
            map.put("rfid",m_rfid.toString());
            map.put("m_code",m_code.toString());
            mallDao.updateMallRfid(map);
        }
        if(rfid != null && !rfid.equals("") ){
            Map<String,Object> map = new HashMap<>();
            map.put("rfid",m_rfid.toString());
            map.put("locate",locate.toString());
            tpDao.updateTPRfid(map);
            Map<String, Object> stringObjectMap = tpDao.selectTPByLocate(locate.toString());
            Map<String,Object> p1 = new HashMap<>();
            p1.put("locate",locate);
            p1.put("tp_code",stringObjectMap.get("TP_CODE"));
            tpDao.updateLocateTP(p1);
        }
        String opCode = getOPCodeUtil.getOPCode();
        TempOp tempOp = new TempOp();
        tempOp.locate = locate.toString();
        if(m_code != null && m_code != null){
            tempOp.m_code = m_code.toString();
        } else {
            tempOp.m_code = "";
        }
        tempOp.sys_code = CustomIdGenerator.generateId("");
        tempOp.op_desc = "OUT";
        tempOp.op_man ="马媛媛";
        tempOp.op_code = opCode;
        tempOp.dt_ins = new Date();
        tempOp.op_exec="0";
//        tempOp.axac_num = 0;
//        tempOp.blur_num = 0;
        //  if (param.get("m_code")!= null)  tempOp.stt_code = param.get("m_code").toString();
        if(param.get("stt_code")!=null && !param.get("stt_code").equals("")){
            tempOp.stt_code = param.get("stt_code").toString();
        }
        if(tempOp.stt_code == null){
            if(sys_code != null){
                tempOp.stt_code = sys_code.toString();
            }else {
                tempOp.stt_code = "";
            }
        }
        if(param.get("m_cubage")!=null && !param.get("m_cubage").equals("")) tempOp.op_num=param.get("m_cubage").toString();

        if(tempOp.op_num==null) tempOp.op_num = "0";
        tempOpDao.save(tempOp);

        Map<String, Object> stringObjectMap = tpDao.selectTPByLocate(locate.toString());
        String tp_code = null;
        if(stringObjectMap != null){
            tp_code = stringObjectMap.get("TP_CODE")!=null?stringObjectMap.get("TP_CODE").toString():"";
        }
        String rtn = getOPCodeUtil.callSpSttJob(opCode,tempOp.stt_code,tempOp.locate,tp_code);
        tempOpDao.saveRealTempOp(tempOp);
        session.setAttribute("currentLocate",tempOp.locate);
        resultObject.message="出库成功";
        return resultObject;
    }

}
